/**
 * Assignment 4                   P4.java                  Due July 13, 2011
 *
 * login: cs11ubn
 */

import java.util.Scanner;

public class P4
{
  /**
   * Driver for P4Rational. Takes user input and evaluates the expression.
   * Does error checking for divide by 0
   */
  public static void main(String[] args) {
    // Declare local variables
    int numer1, denom1, numer2, denom2; // Hold the parameters
    P4Rational a; // The 'right' rational
    P4Rational b; // The 'left' rational
    P4Rational r; // The result of the operation
    char op; // Holds which operation (+ - * /)
    Scanner scan = new Scanner(System.in); // Scanner for input
    String inputStr; // Holds the input from the scanner
    String expr[], operand1Str[], operand2Str[]; // String Array math expr

    // First prompt for input
    System.out.print("Enter fraction (+ - * /) arithmetic ");
    System.out.print("(exit ^D) ");
    System.out.print("\"1/2 + 1/4\": ");

    // Loop while there is more input
    do
    {
      // Loop input error check div by 0
      do
      {
        // Grab the input from the scanner
        inputStr = scan.nextLine();
        expr = inputStr.split(" "); // Get the expression split by spaces

        // Get the first rational from the expression
        operand1Str = expr[0].split("/"); // Math expr operand1
        numer1 = Integer.parseInt(operand1Str[0]);
        denom1 = Integer.parseInt(operand1Str[1]);

        // Get the operator for the expression
        op = expr[1].charAt(0);

        // Get the second rational from the expression
        operand2Str = expr[2].split("/"); // Math expr operand2
        numer2 = Integer.parseInt(operand2Str[0]);
        denom2 = Integer.parseInt(operand2Str[1]);

        // Error check division by zero, prompt again
        if (denom1 == 0 || denom2 == 0 || (numer2 == 0 && op == '/'))
        {
          // Error, output error info and prompt user for more input
          System.out.println("ERROR - Divide by zero");
          System.out.println("");
          System.out.print("Enter fraction (+ - * /) arithmetic ");
          System.out.print("(exit ^D) ");
          System.out.print("\"1/2 + 1/4\": ");
        }
        // Repeat input on error
      } while (denom1 == 0 || denom2 == 0 || (numer2 == 0 && op == '/'));

      // Instantiate 1st fraction, 2 arg ctor
      a = new P4Rational(numer1,denom1);

      // Instantiate 2nd fraction, P4Rational object "b"
      b = new P4Rational(numer2,denom2);

      // Switch the operator to perform the operation and output result
      switch (op)
      {
        case '+': // Addition
          r = a.add(b);
          System.out.println(a.toString() + " + " + b + " = " +
              r.toDoubleString());
          break;
        case '-': // Subtraction
          r = a.sub(b);
          System.out.println(a.toString() + " - " + b + " = " +
              r.toDoubleString());
          break;
        case '*': // Multiplication
          r = a.mult(b);
          System.out.println(a.toString() + " * " + b + " = " +
              r.toDoubleString());
          break;
        case '/': // Division
          r = a.div(b);
          System.out.println(a.toString() + " / " + b + " = " + r.toString() +
              r.toDoubleString());
          break;
        default: // Should not get here, if it does tell the user the error
          System.out.println("ERROR - Invalid operator");
          break;
      }

      // Propmpt again for input
      System.out.println("");
      System.out.print("Enter fraction (+ - * /) arithmetic ");
      System.out.print("(exit ^D) ");
      System.out.print("\"1/2 + 1/4\": ");
    } while( scan.hasNext()); // Check for more input

    // New line for better looking console
    System.out.println("");
    System.exit(0); // Close all connections
  }
}
